package com.panda.framework.netty.handler.extend;


import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import com.alibaba.fastjson.JSON;
import com.panda.business.domain.AccountInfo;
import com.panda.business.service.AccountService;
import com.panda.framework.netty.common.Constant;
import com.panda.framework.netty.utils.MessageUtil;
import com.panda.system.common.constant.EnumConstants;
import com.panda.system.domain.PandaWorkbench;
import com.panda.system.domain.TPandaTaskUser;
import com.panda.system.service.ITPandaTaskUserService;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class GetTodoWebsocketHandler {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    ITPandaTaskUserService todoService;

    @Autowired
    private AccountService accountService;

    /**
     * 获取待办列表
     * @author wechatno:tangjinjinwx
     * @param ctx
     * @param vo
     */
    @Async
    public  void handleMsg(ChannelHandlerContext ctx , TransportMessage vo, String contentJsonStr) {
        try {
            log.debug(contentJsonStr);
            PandaWorkbench req =  JSON.parseObject(contentJsonStr,PandaWorkbench.class);
            if(null != req){
                Long userId = req.getUserId();
                AccountInfo account = accountService.findAccountInfoByid(userId);
                if(null != account){
                    sendJson(ctx, todoService,account);
                }else{
                    MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_PARAMERROR);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
        }
    }

    private static void  sendJson(ChannelHandlerContext ctx, ITPandaTaskUserService todoService,AccountInfo account){
        TPandaTaskUser  todo = new TPandaTaskUser();
        todo.setResponsibleUserId(account.getId());
        todo.setStatus(EnumConstants.UserTaskStatus.OPEN.getCode());
        Calendar calendar = Calendar.getInstance();
        // 0时0分0秒
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        todo.setTastDate(calendar.getTime());

        List<TPandaTaskUser> pandaWorkbenches = todoService.selectTPandaTaskUserList(todo);
        String json = null ;
        if(null != pandaWorkbenches && pandaWorkbenches.size()>0){
            json = JSON.toJSONString(pandaWorkbenches);
        }
        //3、告诉PC客户端消息已收到
        MessageUtil.sendCustomJsonMsg(ctx, "GetTodoResp", json);
    }


}
